#!/bin/bash
# First get their rss
rm zits.rss.work
IMG_LINKS=$(for pagelink in `wget http://www.arcamax.com/zits/channelfeed -O- | 
		 # Get only the real links
		 grep link | sed -e 's|<link>http://www.arcamax.com/zits</link>||' -e 's|http://www.arcamax.com/zits/recent.*||' | 
		 sed -e 's|.*<link>||' -e 's|</link>.*||'`; do 
		# Get the actual link to the image
		echo ${pagelink}_imagelink_$(wget $pagelink -O- | grep "Zits Cartoon for" | sed 's|.*<img src="||' | sed 's|".*||' | sed 's| ||g') ; done)
for img in $IMG_LINKS; do
	echo $img >>zits.rss.work
done

#Make zits.rss.xml from zits.rss.work
cat <<EOF >zits.rss.xml
<rss version="2.0">
    <channel>
        <title>Zits Comic</title>
        <link>http://www.arcamax.com/zits</link>
        <description>Zits Comic</description>
EOF
for img in $(cat zits.rss.work); do
	realImage=$(echo $img | sed 's|.*_imagelink_||')
	realLink=$(echo $img | sed 's|_imagelink_.*||')
	echo "        <item>" >>zits.rss.xml
	echo "            <title>Zits `echo $realLink | cut -d- -f2`</title>" >>zits.rss.xml
	echo "            <link>$realLink</link>" >>zits.rss.xml
	echo "            <description><![CDATA[<img src=\"$realImage\" alt=\"Zits\"></img>]]></description>" >>zits.rss.xml
	echo "        </item>" >>zits.rss.xml
done
cat <<EOF >>zits.rss.xml
    </channel>
</rss>
EOF
exit
